package news.dao.empl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.taglibs.standard.lang.jstl.test.beans.Factory;

import news.dao.DaoFactory;
import news.dao.IBoardDao;
import news.entity.Board;

public class BoardDaoImpl implements IBoardDao {
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public boolean addBoard(Board board) {
		boolean flag=false;
		try {
			conn=DaoFactory.getConn();
			String sql="insert into tb_board values(board_seq.nextval,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, board.getBoardName());
			int result=pstmt.executeUpdate();
			if(result>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}	

	//查询所有
	public List<Board> executeBoard() {
		List<Board> list=new ArrayList<Board>();
		try {
			conn=DaoFactory.getConn();
			String sql="select * from tb_board";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Board board=new Board(rs.getInt("boardid"), rs.getString("boardname"));
				list.add(board);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	//删除
	public boolean deleteBoard(int boardId) {
		boolean flag=false;
		try {
			conn=DaoFactory.getConn();
			String sql="delete from tb_board where boardid=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, boardId);
			int result=pstmt.executeUpdate();
			if(result>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}


	//得到当前以便修改
	public Board getPresent(int boardId) {
		Board board=null;
		try {
			conn=DaoFactory.getConn();
			String sql="select * from tb_board where boardid=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, boardId);
			rs=pstmt.executeQuery();
			if(rs.next()){
				board=new Board(rs.getInt("boardid"), rs.getString("boardName"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return board;
	}


	//更新
	public boolean updateBoard(Board board) {
		boolean flag=false;
		try {
			conn=DaoFactory.getConn();
			String sql="update tb_board set boardname=? where boardid=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(2, board.getBoardId());
			pstmt.setString(1, board.getBoardName());
			int result=pstmt.executeUpdate();
			if(result>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

}
